﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GE_Management_Tool;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using Gem_Tools_Utility;
using GE_Management_Tool.Settings;

namespace Gem_Tools.Settings
{
    public partial class frmA003_Settings_Main : frmBase
    {
        IList<CStaff> m_lsStaff;
        IList<CForm_Role> m_lsForm_Role;
        IList<CForm_Tree> m_lsForm_Tree;
        CForm_Tree m_selected_Form_Tree;
        CStaff m_selected_Staff;
        IDictionary<int, CForm_Role> m_dicRole;

        DataTable m_dt;
        DataTable m_dt_old;

        private void Set_Role_For_User()
        {
            for (int v_i = 0; v_i < m_dt.Rows.Count; v_i++)
            {
                CForm_Role v_role;
                m_dicRole.TryGetValue(CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Auto_ID"]), out v_role);
                if (v_role != null)
                {
                    m_dt.Rows[v_i]["Show"] = v_role.Is_Show;
                    m_dt.Rows[v_i]["Add"] = v_role.Is_Add;
                    m_dt.Rows[v_i]["Update"] = v_role.Is_Update;
                    m_dt.Rows[v_i]["Delete"] = v_role.Is_Delete;
                    m_dt.Rows[v_i]["Exists"] = true;
                }

                m_dicRole.TryGetValue(CUtility.Convert_To_Int32(m_dt_old.Rows[v_i]["Auto_ID"]), out v_role);
                if (v_role != null)
                {
                    m_dt_old.Rows[v_i]["Show"] = v_role.Is_Show;
                    m_dt_old.Rows[v_i]["Add"] = v_role.Is_Add;
                    m_dt_old.Rows[v_i]["Update"] = v_role.Is_Update;
                    m_dt_old.Rows[v_i]["Delete"] = v_role.Is_Delete;
                    m_dt_old.Rows[v_i]["Exists"] = true;
                }
            }
        }

        public frmA003_Settings_Main()
        {
            InitializeComponent();
            CCommon_Function.Format_All(this);
            grvData.OptionsView.ShowGroupPanel = false;
            grvData.OptionsView.ShowAutoFilterRow = false;
            grvData.OptionsView.ShowFooter = false;
        }

        private void Load_Form_Tree()
        {
            //Tạo 1 Data table mới lưu trữ toàn bộ thông tin về role:
            m_dt = new DataTable();
            m_dt.Columns.Add("Auto_ID", typeof(Int32));
            m_dt.Columns.Add("Parent_ID", typeof(Int32));
            m_dt.Columns.Add("Form_ID", typeof(string));
            m_dt.Columns.Add("Stage", typeof(String));
            m_dt.Columns.Add("Show", typeof(Boolean));
            m_dt.Columns.Add("Add", typeof(Boolean));
            m_dt.Columns.Add("Update", typeof(Boolean));
            m_dt.Columns.Add("Delete", typeof(Boolean));
            m_dt.Columns.Add("Exists", typeof(Boolean));

            //Tạo 1 Data table mới lưu trữ toàn bộ thông tin về role_old:
            m_dt_old = new DataTable();
            m_dt_old.Columns.Add("Auto_ID", typeof(Int32));
            m_dt_old.Columns.Add("Parent_ID", typeof(Int32));
            m_dt_old.Columns.Add("Form_ID", typeof(string));
            m_dt_old.Columns.Add("Stage", typeof(String));
            m_dt_old.Columns.Add("Show", typeof(Boolean));
            m_dt_old.Columns.Add("Add", typeof(Boolean));
            m_dt_old.Columns.Add("Update", typeof(Boolean));
            m_dt_old.Columns.Add("Delete", typeof(Boolean));
            m_dt_old.Columns.Add("Exists", typeof(Boolean));

            m_lsForm_Tree = new List<CForm_Tree>();
            CForm_Tree_Controller v_ctrlFormTree = new CForm_Tree_Controller();
            m_lsForm_Tree = v_ctrlFormTree.List_Form_Tree();

            foreach (CForm_Tree v_obj in m_lsForm_Tree)
            {
                DataRow v_row = m_dt.NewRow();
                v_row["Auto_ID"] = v_obj.Auto_ID;
                v_row["Parent_ID"] = v_obj.Parent_ID;
                v_row["Form_ID"] = v_obj.Form_ID;
                v_row["Stage"] = v_obj.Stage;
                v_row["Show"] = true;
                v_row["Add"] = true;
                v_row["Update"] = true;
                v_row["Delete"] = true;
                v_row["Exists"] = false;

                DataRow v_row2 = m_dt_old.NewRow();
                v_row2["Auto_ID"] = v_obj.Auto_ID;
                v_row2["Parent_ID"] = v_obj.Parent_ID;
                v_row2["Form_ID"] = v_obj.Form_ID;
                v_row2["Stage"] = v_obj.Stage;
                v_row2["Show"] = true;
                v_row2["Add"] = true;
                v_row2["Update"] = true;
                v_row2["Delete"] = true;
                v_row2["Exists"] = false;

                m_dt.Rows.Add(v_row);
                m_dt_old.Rows.Add(v_row2);
            }

            trvData.DataSource = m_dt;

            m_dicRole = new Dictionary<int, CForm_Role>();
        }

        private void frmA003_Settings_Main_Load(object sender, EventArgs e)
        {
            try
            {
                
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                frmA0031_Form_Edit_Page v_frm = new frmA0031_Form_Edit_Page();
                if (m_selected_Form_Tree != null)
                {
                    v_frm.Show_To_Create(m_selected_Form_Tree.Auto_ID);
                }
                else
                {
                    v_frm.Show_To_Create(0);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void trvData_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                if (trvData.FocusedNode != null)
                {
                    m_selected_Form_Tree = new CForm_Tree();
                    m_selected_Form_Tree.Auto_ID = CUtility.Convert_To_Int32(trvData.FocusedNode["Auto_ID"]);
                    m_selected_Form_Tree.Parent_ID = CUtility.Convert_To_Int32(trvData.FocusedNode["Parent_ID"]);
                    m_selected_Form_Tree.Stage = trvData.FocusedNode["Stage"].ToString();
                    m_selected_Form_Tree.Form_ID = trvData.FocusedNode["Form_ID"].ToString();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void grvData_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                Load_Form_Tree();
                m_selected_Staff = (CStaff)grvData.GetRow(grvData.FocusedRowHandle);
                if (m_selected_Staff != null)
                {
                    //load danh sách các quyền của user hiện tại:
                    m_lsForm_Role = new List<CForm_Role>();
                    CForm_Role_Controller v_objCtrlForm_Role = new CForm_Role_Controller();
                    m_lsForm_Role = v_objCtrlForm_Role.A003_List_Form_Role_By_Staff_ID(m_selected_Staff.Auto_ID);
                }
                //đưa toàn bộ danh sách quyền vào dictionary để kiểm tra:
                m_dicRole.Clear();
                foreach (CForm_Role v_obj in m_lsForm_Role)
                {
                    m_dicRole.Add(v_obj.Form_Tree_ID, v_obj);
                }
                //Set lại danh sách quyền cho user:
                Set_Role_For_User();

                trvData.RefreshDataSource();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //danh sách các staff
                m_lsStaff = new List<CStaff>();
                CStaff_Controller v_objCtrlStaff = new CStaff_Controller();

                m_lsStaff = v_objCtrlStaff.A003_List_Staff_By_Regency_ID(cbbRegency.SelectedIndex);
                grdData.DataSource = m_lsStaff;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                CForm_Role v_objFormRole = new CForm_Role();
                CForm_Role_Controller v_objCtrlRole = new CForm_Role_Controller();

                for (int v_i = 0; v_i < m_dt.Rows.Count; v_i++)
                {
                    if (CUtility.Convert_To_Bool(m_dt.Rows[v_i]["Show"]) != CUtility.Convert_To_Bool(m_dt_old.Rows[v_i]["Show"]) || 
                        CUtility.Convert_To_Bool(m_dt.Rows[v_i]["Add"]) != CUtility.Convert_To_Bool(m_dt_old.Rows[v_i]["Add"]) ||
                        CUtility.Convert_To_Bool(m_dt.Rows[v_i]["Update"]) != CUtility.Convert_To_Bool(m_dt_old.Rows[v_i]["Update"]) || 
                        CUtility.Convert_To_Bool(m_dt.Rows[v_i]["Delete"]) != CUtility.Convert_To_Bool(m_dt_old.Rows[v_i]["Delete"]))
                    {
                        if (CUtility.Convert_To_Bool(m_dt.Rows[v_i]["Exists"]) == true)
                        {
                            m_dicRole.TryGetValue(CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Auto_ID"]), out v_objFormRole);
                            v_objFormRole.Is_Show = CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Show"]);
                            v_objFormRole.Is_Add = CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Add"]);
                            v_objFormRole.Is_Update = CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Update"]);
                            v_objFormRole.Is_Delete = CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Delete"]);
                            v_objCtrlRole.Update_Form_Role(v_objFormRole);
                        }
                        else
                        {
                            v_objFormRole.Form_Tree_ID = CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Auto_ID"]);
                            v_objFormRole.Staff_ID = CUtility.Convert_To_Int32(m_selected_Staff.Auto_ID);
                            v_objFormRole.Is_Show = CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Show"]);
                            v_objFormRole.Is_Add = CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Add"]);
                            v_objFormRole.Is_Update = CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Update"]);
                            v_objFormRole.Is_Delete = CUtility.Convert_To_Int32(m_dt.Rows[v_i]["Delete"]);
                            v_objCtrlRole.Insert_Form_Role(v_objFormRole);
                        }
                    }

                }
                XtraMessageBox.Show("Save Role Success !");
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }
    }
}